package com;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.asymmetric.RSA;

import java.text.SimpleDateFormat;
import java.util.*;

public class Test {

    public static void main(String[] args) {

        System.out.println("请输入账号：");
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        Boolean login = login(username, null);
        if (!login) {
            System.err.println("没有该用户");
            return;
        }
        System.err.println("请输入密码：");
        Scanner scanner1 = new Scanner(System.in);
        String password = scanner1.nextLine();
        Boolean login1 = login(username, password);
        if (!login1) {
            System.err.println("密码错误");
            return;
        }
        System.err.println("登陆成功，主线程启动");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.err.println("子线程开启，执行kafka心跳检测以及执行日志执行，每五秒执行一次");
                Timer timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        String format = simpleDateFormat.format(new Date());
                        System.err.println(format+"开始执行任务"+"执行kafka心跳检测");
                    }
                },new Date(),1000*5);
            }
        }).start();
        System.err.println("主线程继续执行监听kafka任务");
        System.err.println("模拟10秒来一条数据");
        while(true){
            try {
                Thread.sleep(10000);
                System.err.println("kafka 来了一条数据，我要去处理");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    /**验证账号密码*/
    public static Boolean login (String username,String password){
        Map<String, String> map = new HashMap<>();
        if (map.keySet().size() == 0) {
            String encode = Base64.encode("123456");
            String encode1 = Base64.encode("123321");
            map.put("root",encode);
            map.put("hss",encode1);
        }
        Set<String> strings = map.keySet();
        if (!strings.contains(username)) {
            return false;
        }
        if (strings.contains(username) && password == null) {
            return true;
        }
        String password1 = map.get(username);
        password1 = new String(Base64.decode(password1));
        if (Objects.equals(password1,password)) {
            return true;
        }
        return false;
    }
}
